<template>
	<view class="container">
		<view class="myHeader">
			<view class="alerttop">
				<u-alert type="warning" :description="todayIs"></u-alert>
			</view>
			<u-icon name="account-fill" color="#909399" size="26" @click="gotoUserInfo"></u-icon>
		</view>
		
		<view>
			<u-alert :center="true" type="info" :description="description"></u-alert>
		</view>
		<view class="grid">
			<view class="menuItem bg-blue" @click="turnOnAll()">
				<view class="menuIcon">
					<span class="iconfont icon-24gl-playCircle"></span>
				</view>
				<view class="">
					一键通电
				</view>
			</view>
			<view class="menuItem bg-blue" @click="turnOffAll()">
				<view class="menuIcon">
					<span class="iconfont icon-24gl-stopCircle"></span>
				</view>
				<view>一键断电</view>
			</view>
			<view class="menuItem bg-orange" @click="gotoAddBreak()">
				<view class="menuIcon">
					<span class="iconfont icon-guzhangtousu"></span>
				</view>
				<view>故障报修</view>
			</view>
			<view class="menuItem bg-green" @click="gotoPCList()">
				<view class="menuIcon">
					<span class="iconfont icon-computer"></span>
				</view>
				<view>电脑管理</view>
			</view>
			<view class="menuItem bg-green" @click="gotoESPList()">
				<view class="menuIcon">
					<span class="iconfont icon-zhongguo3chakouchatouchaxiao"></span>
				</view>
				<view>插排管理</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { WSNRTConnect } from '@/static/WSN/WSNRTConnect.js'
	
	const db = uniCloud.database()
	
	export default {
		data() {
			return {
				todayIs: "",
				addr: "api.zhiyun360.com", 
				rtc: null,
				description: "嵌入式软件开发与实训基地",
				espmacInfo:[],
			}
		},
		onLoad() {
			this.todayIs = this.getDate();
			this.getEspmacInfo();
		},
		methods: {
			getConnect(id, key, espmac){
				this.rtc = new WSNRTConnect(id, key)  
				this.rtc.setServerAddr(this.addr)               
				this.rtc.connect()                             
				this.rtc.onConnect = () => {                    
					this.description = '数据服务连接成功...'
					console.log('数据服务连接成功...')
					this.rtc.sendMessage(espmac, '{A1=?,A2=?,D1=?}')    
				}
				this.rtc.onConnectLost = () => {                
					console.log('数据服务连接失败...')
					this.description = '数据服务连接失败...'
				};
				this.rtc.onmessageArrive = (mac, data) => {     
					if (data[0] == '{' && data[data.length - 1] == '}') {     
						let its = data.slice(1,-1).split(',')   
						for (let i = 0; i < its.length; i++) { 
							let t = its[i].split("=")           
							if (t.length != 2) continue		    
							//console.log({mac},its);
							if (mac == espmac) {				
								if (t[0] == 'D1') {          	
									if(t[1] & 15) {
										this.description = "电源已开启"
									} else {
										this.description = "电源已关闭"
									}
								}
							}
						}
					}
				}
			},
			getEspmacInfo(){
				var that = this;
				db.collection('espmac').get().then((res)=>{
					that.espmacInfo = res.result.data
					that.getConnect(that.espmacInfo[0].zonesionId, that.espmacInfo[0].zonesionKey, that.espmacInfo[0].mac);
				})
			},
			getDate(){
				let date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let strDate = date.getDate();
				let weeknum = date.getDay();
				let weeks = ["日", "一", "二", "三", "四", "五", "六"]
				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				let currentdate = "今天是 " + year + "年" + month + "月" + strDate + "日 星期" + weeks[weeknum];
				return currentdate;
			},
			
			turnOnAll(){
				this.rtc.sendMessage(this.espmacInfo[0].mac, '{OD1=15,D1=?}') 
				//this.description = "一键开机成功"
			},
			turnOffAll(){
				this.rtc.sendMessage(this.espmacInfo[0].mac, '{CD1=15,D1=?}') 
			},
			gotoAddBreak(){
				uni.navigateTo({
					url:"/pages/breaks/add"
				})
			},
			gotoPCList(){
				uni.navigateTo({
					url:"/pages/pc-info/list"
				})
			},
			gotoESPList(){
				uni.navigateTo({
					url:"/pages/espmac/list"
				})
			},
			gotoUserInfo(){
				uni.navigateTo({
					url:"/uni_modules/uni-id-pages/pages/userinfo/userinfo"
				})
			}
		}
	}
</script>

<style lang="scss">
	@import url("/static/font/iconfont.css");
	.container{
		display: flex;
		flex-direction: column;
		height: 100vh;
	}
	.myHeader{
		display: flex;
		flex-direction: row;
		margin-bottom: 5px;
		background: #fdf6ec;
		padding: 0 15px 0 5px;
	}
	.alerttop{
		flex: 1;
	}
	.menuItem{
		display: flex;
		flex-direction: row;
		height: 40px;
		margin: 15px;
		padding-left: 15px;
		padding-right: 15px;
		height: 60px;
		line-height: 60px;
		border-radius: 5px;
		color: #333333;
		box-sizing: border-box;
	}
	.menuIcon{
		flex: 1;
	}
	.menuIcon span{
		font-size: 28px;
	}
	.bg-orange {
		background: #fdf6ec;
	}
	.bg-red {
		background: #FF5959;
	}
	.bg-blue {
		background: #ecf5ff;
	}
	.bg-green {
		background: #f5fff0;
	}
	.bg-purple{
		background: #9059FF;
	}
	.bg-pink{
		background: #FFE5EE;
	}
</style>
